% $Header: /cvsroot/html2ps/postscript/image.ps,v 1.1 2005/12/18 07:21:38 Konstantin Exp $

/image-create {                    % => Mask Image Init SY SX
  << >>
  dup /SX    3 index put 
  dup /SY    4 index put 
  dup /Init  5 index put 
  dup /Image 6 index put 
  dup /Mask  7 index put
  
  exch pop
  exch pop
  exch pop
  exch pop
  exch pop
} def

/image-show {                      % => W H Image
% Check if image is transparent
  dup /Mask get /null eq {         % => W H Image
    2 index
    2 index                        % => W H Image W H 
    2 index /SX get
    3 index /SY get
    4 index /Image get
    5 index /Init get              % => W H Image W H SX SY Image Init
    show-image                     % => W H Image
  } {
    2 index
    2 index                        % => W H Image W H 
    2 index /SX get
    3 index /SY get
    4 index /Mask get
    5 index /Image get
    6 index /Init get              % => W H Image W H SX SY Mask Image Init
    show-transparent-image
  } ifelse                         % => W H Image
  pop pop pop
} def

/image-show-simple {               % => Scale Y X Image
  1 index
  3 index moveto                   % => Scale Y X Image

  dup /SX get 4 index mul
  1 index /SY get 5 index mul      % => Scale Y X Image W H
  2 index image-show               % => Scale Y X Image
    
  pop pop pop pop
} def

/image-show-repeat-x {             % => Scale YOffset XOffset Right Width Y X Image
  dup /SX get 8 index mul
  1 index /SY get 9 index mul      % => Scale YOffset XOffset Right width Y X Image W H

% Fill part to the right

  3 index                          % => Scale YOffset XOffset Right width Y X Image W H CX
  {                                % => Scale YOffset XOffset Right width Y X Image W H CX
    dup
    8 index ge { exit } if

    dup 6 index moveto             % => Scale YOffset XOffset Right width Y X Image W H CX

    2 index 2 index 5 index
    image-show                     % => Scale YOffset XOffset Right width Y X Image W H CY
    6 index add                    % => Scale YOffset XOffset Right width Y X Image W H CY
  } loop
  pop
% Fill part to the left
  
  3 index
  {                                % => Scale YOffset XOffset Right width Y X Image W H CX
    dup 7 index add            
    5 index 10 index sub
    lt { exit } if

    dup 7 index sub
    6 index moveto                 % => Scale YOffset XOffset Right width Y X Image W H CX

    2 index 2 index 5 index
    image-show                     % => Scale YOffset XOffset Right width Y X Image W H CX
    
    6 index sub                    % => Scale YOffset XOffset Right width Y X Image W H CX
  } loop
  pop
  pop pop

  pop pop pop
  pop pop pop
  pop pop
} def

/image-show-repeat-y {             % => Scale YOffset XOffset Bottom height Y X Image
  dup /SX get 8 index mul
  1 index /SY get 9 index mul      % => Scale YOffset XOffset Bottom height Y X Image W H

% Fill part to the bottom

  4 index                          % => Scale YOffset XOffset Bottom height Y X Image W H CY
  {                                % => Scale YOffset XOffset Bottom height Y X Image W H CY
    6 index 1 index add
    8 index le { exit } if

    4 index 1 index moveto         % => Scale YOffset XOffset Bottom height Y X Image W H CY

    2 index 2 index 5 index
    image-show                     % => Scale YOffset XOffset Bottom height Y X Image W H CY
    6 index sub                    % => Scale YOffset XOffset Bottom height Y X Image W H CY
  } loop
  pop

% Fill part to the top
  
  4 index
  {                                % => Scale YOffset XOffset Bottom height Y X Image W H CY
    6 index 1 index exch sub
    10 index 7 index add
    ge { exit } if

    4 index 1 index moveto         % => Scale YOffset XOffset Bottom height Y X Image W H CY

    2 index 2 index 5 index
    image-show                     % => Scale YOffset XOffset Bottom height Y X Image W H CY
    
    6 index add                    % => Scale YOffset XOffset Bottom height Y X Image W H CY
  } loop
  pop
  pop pop
    
  pop pop pop
  pop pop pop
  pop pop
} def

/image-show-repeat-xy {            % => Scale YOffset XOffset Bottom Right Height Width Y X Image
  dup /SX get 10 index mul
  1 index /SY get 11 index mul      % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H

% Fill bottom-right quadrant
  4 index                          % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  {
    dup 8 index add
    10 index le { exit } if        % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY

    4 index                        % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    {
      dup 10 index ge { exit } if

      dup 2 index moveto
      3 index 3 index 6 index
      image-show

      7 index add
    } loop                         % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    pop

    7 index sub
  } loop                           % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  pop

% Fill bottom-left quadrant
  4 index                          % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  {
    dup 8 index add
    10 index le { exit } if        % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY

    4 index                        % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    {
      dup 8 index add
      6 index 13 index sub
      le { exit } if

      dup 2 index moveto
      3 index 3 index 6 index
      image-show

      7 index sub
    } loop                         % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    pop

    7 index sub
  } loop                           % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  pop

% Fill top-right quadrant
  4 index                          % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  {
    dup 
    6 index 13 index add
    gt { exit } if                 % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY

    4 index                        % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    {
      dup 10 index gt { exit } if

      dup 2 index moveto
      3 index 3 index 6 index
      image-show

      7 index add
    } loop                         % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    pop

    7 index add
  } loop                           % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  pop

% Fill top-left quadrant
  4 index                          % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  {
    dup 
    6 index 13 index add
    ge { exit } if                 % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY

    4 index                        % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    {
      dup 8 index add
      6 index 13 index sub
      le { exit } if

      dup 2 index moveto
      3 index 3 index 6 index
      image-show

      7 index sub
    } loop                         % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY CX
    pop

    7 index add
  } loop                           % => Scale YOffset XOffset Bottom Right Height Width Y X Image W H CY
  pop

  pop pop
  
  pop pop pop
  pop pop pop
  pop pop pop
  pop
} def

/show-image {                      % => W H W H Samples InitFunc
  exec                             % => W H W H Samples
  
  gsave

  matrix                           % => W H W H Samples Matr
  currentpoint                     % => W H W H Samples Matr X Y
  translate                        % => W H W H Samples Matr'
  5 index
  4 index div                      % => W H W H Samples Matr' KX
  5 index
  4 index div neg                  % => W H W H Samples Matr' KX KY
  scale                            % => W H W H Samples Matr''
  0 3 index neg                    % => W H W H Samples Matr'' 0 -H(px)
  translate                        % => W H W H Samples Matr

  <<                                
    /ImageType        1
    /Width            7 index
    /Height           8 index
    /BitsPerComponent 8
    /Decode           [0 1 0 1 0 1]
    /ImageMatrix      12 index
    /DataSource       15 index
  >>

  image

  grestore

  pop pop pop 
  pop pop pop
} def

/show-transparent-image {          % => W H W H Mask Samples InitFunc
  exec                             % => W H W H Mask Samples
    
  gsave

  matrix                           % => W H W H Mask Samples Matr
  currentpoint         % => W H W H Mask Samples Matr X Y
  translate            % => W H W H Mask Samples Matr'
  6 index
  5 index div          % => W H W H Mask Samples Matr' KX
  6 index
  5 index div neg      % => W H W H Mask Samples Matr' KX KY
  scale                % => W H W H Mask Samples Matr''
  0 4 index neg        % => W H W H Mask Samples Matr'' 0 -H(px)
  translate            % => W H W H Mask Samples Matr
          
% As support of type 3 images in ps2pdf is bad, we'll use this workaround
  ps2pdf-transparency-hack {
    <<                                
      /ImageType        no-transparency-output { 1 } { 4 } ifelse
      /Width            8 index
      /Height           9 index
      /BitsPerComponent 8
      /Decode           [0 1 0 1 0 1]
      /ImageMatrix      12 index
      /DataSource       15 index
      /MaskColor        [0 0 0]
    >>
  } {
    <<
      /ImageType        3
      /InterleaveType   3
      /DataDict         
      <<
        /ImageType        1
        /Width            14 index
        /Height           15 index
        /BitsPerComponent 8
        /Decode           [0 1 0 1 0 1]
        /ImageMatrix      18 index
        /DataSource       21 index
      >>
      /MaskDict 
      <<
        /ImageType        1
        /Width            16 index
        /Height           17 index
        /BitsPerComponent 1
        /Decode           [0 1]
        /ImageMatrix      20 index
        /DataSource       24 index
      >>
    >>
  } ifelse

  image

  grestore

  pop pop pop pop 
  pop pop pop
} def

